package com.example.demo.controller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.Workorder;
import com.example.demo.service.WorkorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class WorkorderController {
    @Autowired
    WorkorderService workorderService;

    @GetMapping("/wo")
    public List<Workorder> selectAll(){
        return workorderService.selectAll();
    }

    @PostMapping("/wo/search")
    public Workorder search(@RequestBody Workorder workorder){
        int id = workorder.getOrder_id();
        Workorder workorder1 = workorderService.selectById(id);
        if(workorder1 != null){
            return workorderService.selectById(id);
        }else{
            return null;
        }
    }

    @PostMapping("/wo/search_bus/{id}")
    public List<Workorder> search_bus(@PathVariable int id){
        return workorderService.selectBybusId(id);
    }

    @PostMapping("/wo/add")
    public Result add(@RequestBody Workorder workorder){
        if (workorder != null){
            workorderService.addWorkorder(workorder);
            return Result.success();
        }else{
            return Result.error("添加失败");
        }
    }

    @PutMapping("/wo/update/{id}")
    public Result update(@RequestBody Workorder workorder,@PathVariable int id){
        if(workorder != null){
            workorderService.updateWorkorder(workorder);
            return Result.success();
        }else{
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/wo/delete/{id}")
    public Result delete(@PathVariable int id){
        if(id > 0){
            workorderService.deleteWorkorder(id);
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }

    @PostMapping("/wo/import")
    public Result importWorkorder(@RequestBody List<Workorder> workorders){
        if(workorders != null ){
            workorderService.changeAllWorkorder(workorders);
            return  Result.success();
        }else{
            return Result.error("导入失败");
        }
    }

    @GetMapping("/wo/order_num")
    public Result order_num() {
        int num = workorderService.order_num();
        if (num != 0){
            workorderService.order_num();
            return Result.success(num);
        }
        return Result.error("获取总工单数失败");
    }

    @GetMapping("/wo/update_performance")
    public Result update_performance(){
        workorderService.update_performance();
        return Result.success();
    }

}
